package cn.appsys.tools;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.time.DateFormatUtils;

import com.alibaba.fastjson.serializer.ValueFilter;

public class MyJsonDateFilter implements ValueFilter {
	
	// 映射字段与格式的关系（key是正则表达式，�?�是日期格式�?
	// 示例{".*Date" : "yyyy-MM-dd" , "birthday":"yyyy-MM-dd" , ".*DateTime" : "yyyy-MM-dd HH:mm:ss"}
	private Map<String, String> dateMappings = new HashMap<String, String>();
	
	@Override
	public Object process(Object object, String name, Object value) {
		if(value instanceof Date){
			//如果值是Date格式，我们才进行处理
			Set<String> keys = getDateMappings().keySet();
			// 迭代�?有的key，也就是正则表达�?
			for (String key : keys) {
				if(name.matches(key)){
					// 通过正则验证的格式�??
					String pattern = getDateMappings().get(key);
					return DateFormatUtils.format((Date)value, pattern);
				}
			}
		}
		return value;
	}

	public Map<String, String> getDateMappings() {
		return dateMappings;
	}

	public void setDateMappings(Map<String, String> dateMappings) {
		this.dateMappings = dateMappings;
	}
}